<?php
if (!file_exists('../install.lock')) {
    header('Location: ../install.php');
    exit;
}
session_start();
require_once '../config.php';

// 检查是否登录
if (!isset($_SESSION['admin_id'])) {
    header('Location: login.php');
    exit;
}

// 获取管理员信息
$stmt = $pdo->prepare("SELECT * FROM admins WHERE id = ?");
$stmt->execute([$_SESSION['admin_id']]);
$admin = $stmt->fetch();

$success = '';
$error = '';

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    try {
        $pdo->beginTransaction();

        // 处理普通设置项
        if (isset($_POST['settings'])) {
            foreach ($_POST['settings'] as $name => $value) {
                $stmt = $pdo->prepare("INSERT INTO settings (name, value) VALUES (?, ?) ON DUPLICATE KEY UPDATE value = ?");
                $stmt->execute([$name, $value, $value]);
            }
        }
        
        $pdo->commit();
        $success = "设置已成功保存";
    } catch (Exception $e) {
        $pdo->rollBack();
        $error = "保存设置失败：" . $e->getMessage();
    }
    // 重新获取设置和管理员信息
    $settings = getSettings();
    $stmt = $pdo->prepare("SELECT * FROM admins WHERE id = ?");
    $stmt->execute([$_SESSION['admin_id']]);
    $admin = $stmt->fetch();
}
?>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
    <title><?php echo htmlspecialchars($settings['title'] ?? '系统设置'); ?> - 后台管理</title>
    <link rel="shortcut icon" type="image/x-icon" href="../assets/favicon.ico">
    <link rel="stylesheet" type="text/css" href="../assets/css/materialdesignicons.min.css">
    <link rel="stylesheet" type="text/css" href="../assets/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="../assets/css/animate.min.css">
    <link rel="stylesheet" type="text/css" href="../assets/css/style.min.css">
</head>
<body>
<div id="lyear-preloader" class="loading">
    <div class="ctn-preloader"><div class="round_spinner"><div class="spinner"></div><img src="../assets/images/loading-logo.png" alt=""></div></div>
</div>

<div class="lyear-layout-web">
    <div class="lyear-layout-container">
        <?php $current_page = 'settings'; include 'navbar.php'; ?>
        
        <main class="lyear-layout-content">
            <div class="container-fluid p-t-15">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-header"><h4>系统设置</h4></div>
                            <div class="card-body">
                                <?php if ($success): ?>
                                <div class="alert alert-success alert-dismissible fade show" role="alert">
                                    <?php echo $success; ?>
                                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                                </div>
                                <?php endif; ?>
                                <?php if ($error): ?>
                                <div class="alert alert-danger alert-dismissible fade show" role="alert">
                                    <?php echo $error; ?>
                                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                                </div>
                                <?php endif; ?>

                                <ul class="nav nav-tabs" id="myTab" role="tablist">
                                    <li class="nav-item" role="presentation">
                                        <button class="nav-link active" id="system-tab" data-bs-toggle="tab" data-bs-target="#system" type="button" role="tab" aria-controls="system" aria-selected="true">基本设置</button>
                                    </li>
                                    <li class="nav-item" role="presentation">
                                        <button class="nav-link" id="alipay-tab" data-bs-toggle="tab" data-bs-target="#alipay" type="button" role="tab" aria-controls="alipay" aria-selected="false">支付配置</button>
                                    </li>
                                </ul>
                                
                                <form method="post" action="settings.php">
                                    <div class="tab-content m-t-15" id="myTabContent">
                                        <!-- 基本设置 -->
                                        <div class="tab-pane fade show active" id="system" role="tabpanel" aria-labelledby="system-tab">
                                            <div class="form-group row mb-4">
                                                <label class="col-sm-2 col-form-label" for="title">网站标题</label>
                                                <div class="col-sm-8">
                                                    <input type="text" class="form-control" id="title" name="settings[title]" value="<?php echo htmlspecialchars($settings['title'] ?? ''); ?>">
                                                </div>
                                            </div>
                                            <div class="form-group row mb-4">
                                                <label class="col-sm-2 col-form-label" for="home_title">首页标题</label>
                                                <div class="col-sm-8">
                                                    <input type="text" class="form-control" id="home_title" name="settings[home_title]" value="<?php echo htmlspecialchars($settings['home_title'] ?? ''); ?>">
                                                </div>
                                            </div>
                                            <div class="form-group row mb-4">
                                                <label class="col-sm-2 col-form-label" for="home_intro">首页介绍</label>
                                                <div class="col-sm-8">
                                                    <input type="text" class="form-control" id="home_intro" name="settings[home_intro]" value="<?php echo htmlspecialchars($settings['home_intro'] ?? ''); ?>">
                                                </div>
                                            </div>
                                            <div class="form-group row mb-4">
                                                <label class="col-sm-2 col-form-label" for="pop_text">弹窗内容</label>
                                                <div class="col-sm-8">
                                                    <textarea class="form-control" id="pop_text" name="settings[pop_text]" rows="3"><?php echo htmlspecialchars($settings['pop_text'] ?? ''); ?></textarea>
                                                </div>
                                            </div>
                                            <div class="form-group row mb-4">
                                                <label class="col-sm-2 col-form-label" for="music_url">音乐链接</label>
                                                <div class="col-sm-8">
                                                    <input type="text" class="form-control" id="music_url" name="settings[music_url]" value="<?php echo htmlspecialchars($settings['music_url'] ?? ''); ?>">
                                                </div>
                                            </div>
                                            <div class="form-group row mb-4">
                                                <label class="col-sm-2 col-form-label" for="km_url">购卡链接</label>
                                                <div class="col-sm-8">
                                                    <input type="text" class="form-control" id="km_url" name="settings[km_url]" value="<?php echo htmlspecialchars($settings['km_url'] ?? ''); ?>">
                                                </div>
                                            </div>
                                            <div class="form-group row mb-4">
                                                <label class="col-sm-2 col-form-label" for="qb_url">Q绑接口</label>
                                                <div class="col-sm-8">
                                                    <input type="text" class="form-control" id="qb_url" name="settings[qb_url]" value="<?php echo htmlspecialchars($settings['qb_url'] ?? ''); ?>">
                                                </div>
                                            </div>
                                            <div class="form-group row mb-4">
                                                <label class="col-sm-2 col-form-label" for="time">运行时间</label>
                                                <div class="col-sm-8">
                                                    <input type="text" class="form-control" id="time" name="settings[time]" value="<?php echo htmlspecialchars($settings['time'] ?? ''); ?>">
                                                </div>
                                            </div>
                                            <div class="form-group row mb-4">
                                                <label class="col-sm-2 col-form-label" for="copyright">版权信息</label>
                                                <div class="col-sm-8">
                                                    <input type="text" class="form-control" id="copyright" name="settings[copyright]" value="<?php echo htmlspecialchars($settings['copyright'] ?? ''); ?>">
                                                </div>
                                            </div>
                                        </div>
                                        
                                        <!-- 支付配置 -->
                                        <div class="tab-pane fade" id="alipay" role="tabpanel" aria-labelledby="alipay-tab">
                                            <div class="mb-4">
                                                <a href="/pay/test.php" target="_blank" class="btn btn-info">在线支付测试</a>
                                            </div>
                                             <div class="mb-4">
                                                <a href="https://www.eyoucms.com/help/plus/31357.html" target="_blank" class="btn btn-info">获取支付宝当面付配置教程</a>
                                            </div>
                                            <div class="form-group row mb-4">
                                                <label class="col-sm-2 col-form-label" for="alipay_app_id">支付宝 APP ID</label>
                                                <div class="col-sm-8">
                                                    <input type="text" class="form-control" id="alipay_app_id" name="settings[alipay_app_id]" value="<?php echo htmlspecialchars($settings['alipay_app_id'] ?? ''); ?>">
                                                </div>
                                            </div>
                                            <div class="form-group row mb-4">
                                                <label class="col-sm-2 col-form-label" for="alipay_private_key">应用私钥</label>
                                                <div class="col-sm-8">
                                                    <textarea class="form-control" id="alipay_private_key" name="settings[alipay_private_key]" rows="15"><?php echo htmlspecialchars($settings['alipay_private_key'] ?? ''); ?></textarea>
                                                </div>
                                            </div>
                                            <div class="form-group row mb-4">
                                                <label class="col-sm-2 col-form-label" for="alipay_public_key">支付宝公钥</label>
                                                <div class="col-sm-8">
                                                    <textarea class="form-control" id="alipay_public_key" name="settings[alipay_public_key]" rows="5"><?php echo htmlspecialchars($settings['alipay_public_key'] ?? ''); ?></textarea>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div class="form-group row m-t-10">
                                        <div class="offset-sm-2 col-sm-8">
                                            <button type="submit" class="btn btn-primary">保存设置</button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
    </div>
</div>

<script type="text/javascript" src="../assets/js/jquery.min.js"></script>
<script type="text/javascript" src="../assets/js/popper.min.js"></script>
<script type="text/javascript" src="../assets/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../assets/js/perfect-scrollbar.min.js"></script>
<script type="text/javascript" src="../assets/js/main.min.js"></script>
</body>
</html> 